﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using NHibernate.Criterion;
using Project.Common;
using Project.Model;

namespace Web
{
	/// <summary>
	/// 功能：分页列表页面
	///       (Column)
	/// 作者：luky
	/// 日期：2010-12-23
	/// </summary>
    public partial class Column_List : AdminPage
    {
        //创建查询器
        IList<ICriterion> queryList = new List<ICriterion>();
        // 排序字段
        Order order;

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                this.QuerySelect();
            }
        }
        #region 搜索

        protected void searchButton_Click(object sender, EventArgs e)
        {
            QuerySelect();
        }

        private void QuerySelect()
        {
            // 传入条件
            // 添加自定义条件
            if (!string.IsNullOrEmpty(txt_Id.Text))
                queryList.Add(Restrictions.Eq(PropertyNameHelper.Resolve<Column>(d => d.Id), txt_Id.Text));

            BindGrid();
        }

        #endregion

        private void BindGrid()
        {
            AspNetPager1.RecordCount = ManagerFactory.ColumnManager.GetCount(queryList);
            IList<Column> list = ManagerFactory.ColumnManager.GetPageList(AspNetPager1, queryList, order);
            GridView1.DataSource = list;
            GridView1.DataBind();
        }

        #region gridview绑定及命令事件

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            object itemId = e.CommandArgument;
            if (e.CommandName == "DeleteIt")
            {
                #region 约束检查
                bool IsExistFK = false;
                #endregion
                if (!IsExistFK)
                {
                    ManagerFactory.ColumnManager.Delete(int.Parse(itemId.ToString()));
                    this.QuerySelect();
                }
                else
                {
                    ExecJsAjax("alert('该记录已经被使用,不能删除！');", Page);
                }
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //e.Row.Attributes.Add("onmouseout", "this.className=this.className.replace(new RegExp(\"( ?|^)gridSelectedItem\\\\b\"),\"\");");
                //e.Row.Attributes.Add("onmouseover", "this.className+='" + " gridSelectedItem" + "'");
            }
        }

        #endregion

        #region 分页

        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            this.QuerySelect();
        }

        #endregion

        #region 排序

        public string SortD
        {
            get
            {
                if (ViewState["SortD"] == null)
                {
                    switch (GridView1.SortDirection)
                    {
                        case SortDirection.Ascending:
                            ViewState["SortD"] = "ASC";
                            break;
                        case SortDirection.Descending:
                            ViewState["SortD"] = "DESC";
                            break;
                    }
                }
                return Convert.ToString(ViewState["SortD"]);
            }
            set
            {
                ViewState["SortD"] = value;
            }
        }
        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            SortD = (SortD.ToLower() == "asc" ? "DESC" : "ASC");
            order = new Order(e.SortExpression,SortD.ToLower()=="ASC".ToLower());
        }
        protected void GridView1_Sorted(object sender, EventArgs e)
        {
            this.QuerySelect();
        }
       
        #endregion

    }
}
